{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from src.io.psee_loader import PSEELoader"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Opening a file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# open a file\n",
    "video = PSEELoader(\"some_file_td.dat\")\n",
    "print(video)  # show some metadata\n",
    "video.event_count()  # number of events in the file\n",
    "video.total_time()  # duration of the file in mus"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Event Format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's read some Events , there are two ways by number of events or by time slices\n",
    "events = video.load_n_events(10)  # this loads the 10 next events\n",
    "events"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Events are in a numpy [structured array](https://docs.scipy.org/doc/numpy/user/basics.rec.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# TLDR : we can access different fields in this one dimensional array\n",
    "print(events['t'])  # this shows only the timestamps of events\n",
    "# for instance to count the events of positive polarity you can do :\n",
    "np.sum(events['p'] > 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's randomly drop some events\n",
    "np.random.choice(events, len(events)//2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's now try to read 10ms worth of events\n",
    "events = video.load_delta_t(10000)\n",
    "events"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Iteration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# to iterate through a dataset you can use :\n",
    "while not video.done:\n",
    "    # load events and boxes from all files\n",
    "    events = video.load_delta_t(50000)\n",
    "    # do something with the events"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# once a video is fully read there is no events in it anymore :\n",
    "print(len(video.load_n_events(1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# but you can rewind using seek_event or seek_time\n",
    "video.seek_event(3)  # this goes before the fourth events (index starts at 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "video.seek_time(10000)  # this goes before the first event after 9999mus"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bonus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# this functions can be used to read a video backwards for instance\n",
    "video.seek_time(video.total_time()+1)\n",
    "delta_t = 100000\n",
    "for t in np.arange(video.total_time()- delta_t, -delta_t, -delta_t):\n",
    "    video.seek_time(t)\n",
    "    events = video.load_delta_t(delta_t)\n",
    "    # they should be sorted in descending timestamp order !\n",
    "    events = events[::-1]\n",
    "    # do some cunning computer vision here.\n",
    "    pass"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
